//
//  RootViewController.m
//  mock-store
//
//  Created by aewatson on 10/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RootViewController.h"


@implementation RootViewController

@synthesize cart;
@synthesize toolbar;

-(void) btnViewCartClicked {
    // initialize cart view controller
    CartViewController *cartViewController = [[CartViewController alloc] initWithNibName:@"CartViewController" bundle:nil];
    // point to cart
    cartViewController.cart = cart;
    
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:cartViewController animated:YES];
    [cartViewController release];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    places = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Places" ofType:@"plist"]];
    [places retain];
    
    // initialize cart
    cart = [[[Cart alloc] init] retain];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // create toolbar
    toolbar = [[UIToolbar alloc] init];
    toolbar.barStyle = UIBarStyleDefault;
    toolbar.frame = CGRectMake(0, 436, 320, 50);    
    //Set the toolbar to fit the width of the app.
    [toolbar sizeToFit];
    // create the array to hold the buttons, which then gets added to the toolbar
    NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:1];


    // determine if any buttons should appear in the toolbar
    if ([cart getLineItemCount] > 0) {
                
        // create view cart button
        UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithTitle:@"View Cart" style:UIBarButtonItemStyleBordered target:self action:@selector(btnViewCartClicked)];
        [buttons addObject:bi];
        [bi release];
    }
    
    // add buttons to toolbar
    [toolbar setItems:buttons];
    
    // add toolbar to view
    [self.navigationController.view addSubview:toolbar];

}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
    toolbar.items = nil;
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 */

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [places count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.
    int row = indexPath.row;
    NSDictionary *place = [places objectAtIndex:row];
    NSString *widgetName = [place objectForKey:@"name"];
    NSNumber *widgetPrice = [place objectForKey:@"price"];
    cell.textLabel.text = widgetName;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"$%0.2f",[widgetPrice floatValue]];
    return cell;

}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ItemViewController *itemViewController = [[ItemViewController alloc] initWithNibName:@"ItemViewController" bundle:nil];
    int row = indexPath.row;
    NSDictionary *place = [places objectAtIndex:row];
    // Create item object from item data
    Item *item = [[Item alloc] initWithSKU:[place objectForKey:@"SKU"] name:[place objectForKey:@"name"] description:[place objectForKey:@"description"] andPrice:[place objectForKey:@"price"]];
    // set view controller data members, item and cart
    itemViewController.item = item;
    itemViewController.cart = cart;
    
    // ...
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:itemViewController animated:YES];
    [itemViewController release];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}

- (void)dealloc
{
    [super dealloc];
}

@end
